﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_PositionDuties : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IPositionBLL posBLL = new PositionBLL();
        private IPositionDutyBLL dutyBLL = new PositionDutyBLL();
        private IPositionDutyDtlsBLL dtlsBLL = new PositionDutyDtlsBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        private PositionInfo posInfo = new PositionInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;

                lblUpperText.Text = "กำหนดหน้าที่ความรับผิดชอบ";
                RadGrid1.Visible = selected_node != null;
            }
        }
        public void LoadDataGrid(Nullable<int> PosSeq)
        {
            IList<PositionDutyDtlsInfo> Info = dtlsBLL.LoadGridPositionDutyDtls(PosSeq);
            RadGrid1.DataSource = Info;
            RadGrid1.DataBind();
        }
        private void BindTree(RadTreeView treeView)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTreeView(1, "");

            treeView.DataSource = infoList;
            treeView.DataBind();
        }

        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTreeView(1, "");

            foreach (TreeViewInfo info in treeBLL.LoadPositionTreeView(1, ""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadPositionTreeView(1, e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadPositionTreeView(1, e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }

        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTreeView(1, "");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();

            //RadTreeView1.Nodes.Clear();
            //if (txtSearch.Text.Length > 0)
            //{
            //    RadTreeView1.DataSource = bll.GetHashCode(txtSearch.Text);
            //    RadTreeView1.DataBind();
            //    RadTreeView1.ExpandAllNodes();
            //    lblResult.Text = RadTreeView1.GetAllNodes().Count.ToString();
            //    PanelSearch.Visible = true;
            //}
            //else
            //{
            //    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            //    PanelSearch.Visible = false;
            //}
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();

            //RadTreeView1.Nodes.Clear();
            //LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            //PanelSearch.Visible = false;
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            string value = selected_node.Value;
            string[] root_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

            if (!string.IsNullOrEmpty(value))
            {
                RadGrid1.Visible = selected_node.Level == 2;

                if (selected_node.Level == 2)
                {
                    lblPosSeq.Text = root_value[1];
                    LoadDataGrid(root_value[1].Length > 0 ? Int32.Parse(root_value[1]) : (int?)null);

                    IList<PositionInfo> infoList = posBLL.ListPositionsBySequence(lblPosSeq.Text.Trim());

                    if (infoList.Count > 0)
                        lblUpperText.Text = "หน้าที่ความรับผิดชอบตำแหน่ง " + infoList[0].Name;
                }
                lblUpperText.Text = "กำหนดหน้าที่ความรับผิดชอบ";
            }

        }

        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            //if (e.Argument == "CloseWindow")
            //{
            //    if (DutyControl1.Visible)
            //    {
            //        DutyControl1.LoadFieldPositionData(Int32.Parse(Session["FieldSeq"].ToString()));
            //        DutyControl1.LoadFieldDutyData(1);
            //    }
            //    if (DutyDtlsControl1.Visible)
            //    {

            //    }

            //    RefreshTree();
            //}
            //else
            //{
            //    if (DutyControl1.Visible)
            //    {
            //        DutyControl1.GetKeyValue(e.Argument);
            //    }
            //    if (DutyDtlsControl1.Visible)
            //    {
            //        //DutyDtlsControl1.GetKeyValue(e.Argument);
            //    }
            //}
        }

        public void RefreshTree()
        {
            //RadTreeNode selected_node = RadTreeView1.SelectedNode;
            //if (selected_node == null)
            //{
            //    selected_node = RadTreeView1.FindNodeByValue("0");
            //}
            //RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            //RadManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            //PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            //RadManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            PositionDutyDtlsInfo Info = new PositionDutyDtlsInfo();
            Session["PosSeq"] = lblPosSeq.Text;

            if (e.CommandName == "Delete")
            {
                string seq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Seq"].ToString();

                Info.Seq = seq.Length > 0 ? Int32.Parse(seq) : (int?)null;
                Info.UpdatedBy = ssMng.EmpSeqI;

                dtlsBLL.Delete(Info);
            }

            LoadDataGrid(lblPosSeq.Text.Length > 0 ? Int32.Parse(lblPosSeq.Text) : (int?)null);
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.GetPositionResultSearch(1, txtSearch.Text);

            RadTreeView1.Nodes.Clear();
            if (txtSearch.Text.Length > 0)
            {
                RadTreeView1.DataSource = infoList;
                RadTreeView1.DataBind();
                RadTreeView1.ExpandAllNodes();
                //lblResult.Text = RadTreeView1.GetAllNodes().Count.ToString();
                //PanelSearch.Visible = true;
            }
            else
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                //PanelSearch.Visible = false;
            }
        }

        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTreeView(1, "");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
    }
}
